// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129

const mysql = require("mysql");
const reptile = require("./reptile");

function success(data) {
	return {
		...data,
		code: 200
	}
}

function fail(message, code) {
	return {
		code: code || 201,
		message
	}
}

function checkParms(obj, keys) {
	if (!obj) return false;
	for (let v of keys)
		if (!obj[v])
			return false;
	return true
}

module.exports = {
	_before: function() { // 通用预处理器

	},
	/**
	 * method1方法描述
	 * @returns {number} 返回值描述
	 */
	async setting(data) {
		// 参数校验，如无参数则不需要
		if (!data || (!data?.key && !data?.pageIndex)) {
			return fail('缺少参数')
		}
		if (data?.pageIndex)
			return success({
				list: await reptile.setting(null, null, (data.pageIndex - 1), data?.pageSize || 15)
			})

		if (data?.key)
			return success({
				data: await reptile.setting(data.key, data?.value)
			})

		return fail('未知错误')
	},
	async reClass() {
		return await reptile.reClass()
	},
	async reqPage(data) {
		if (!checkParms(data, ['pageIndex']))
			return fail('缺少参数')
		return success({
			list: await reptile.reqPage(data.pageIndex, data?.h)
		})
	},
	async videoList(data) {
		return success({
			...(await reptile.videoList(
				data?.type_id_1,
				data?.type_id,
				data?.pageIndex || 1,
				data?.pageSize || 15,
				data?.ids
			))
		})
	},
	async rebuild(data) {
		return await reptile.rebuild()
	}
}